<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	
	//判断是否已经登录
	public function loging(){
		
		if(session('?username')&&session('username')!=''){
			
			
			
		}else{
			$this->redirect('Home/Index/login');
		}
		
		
		
	}
	
	//显示首页
    public function index(){
     
    $this->redirect('Home/Index/show');
	}
	
	//显示导航页面
	public function show(){
		$this->loging();
		
		$time=time();
		$this->assign('time',$time);
		$this->display();	
		
		
	}
	//显示登录页面
	public function login(){
		
		
		$this->display();
		
		
	}
	//显示修改密码页面
	public function logpwd(){
		
		
		$this->display();
		
		
	}
	//修改密码是否成功
	public function chpwd(){
		
		
		$pwd=D('Pwd');
		
		if($data=$pwd->create()){
			$where['username']=$data['username'];
			$where['password']=$_POST['oldpwd'];
			
			$user=M('user');
			$ud=$user->where($where)->find();
			if(!empty($ud)){
				
				$save['id']=$ud['id'];
				$save['password']=$data['password'];
			
				 $num=$user->save($save);
				 
				 if ($num>0){
					 
					 $this->success('更新密码成功，请登录','logout');
				 }else{
					 
					 $this->error("更新密码失败");
					 
				 }
			}else{
				
				$this->error("用户密码不对应");
			}
			
			
			
			
		}else{
			$mes=$pwd->getError();
			$this->error("修改失败，$mes");
		};
		
		
	}
	//注册页面
	public function log(){
		
		$this->display();
		
		
	}
	//注册是否成功
	public function dolog(){
		$m=D('User');
		
		if($m->create()&& $m->add()!=0){
			
			$this->success('注册成功','./login');
		}else{
			$mes=$m->getError();
			$this->error("注册失败，$mes");
		};
		
		
		
		
	}
	//登录是否成功
	public function dologin(){
		$u=M('User');
		
		$name= $_POST['username'];
		$pwd =$_POST['password'];
		$where['username']=$name;
		$user=$u->where($where)->find();
		if($user['password']==$pwd){
			
			session('username',$name);
			session('uid',$user['id']);
			$this->success('登陆成功','show');
			
		}else{
			$this->error('登录失败！！！！');
		}
		}
	//退出登录
	public function logout(){

		
			
		$_SESSION = array(); //清除SESSION值.
        if(isset($_COOKIE[session_name()])){  //判断客户端的cookie文件是否存在,存在的话将其设置为过期.
                setcookie(session_name(),'',time()-1,'/');
        }
                session_destroy();  //清除服务器的sesion文件
                $this->redirect('Home/Index/login');
	}	
	//显示所有检测证书
	public function show_ic(){


		$ic=M('Ic');
		$data=$ic->select();
		$this->assign('data',$data);
		$this->display();
			
			
	}	
//显示仪器详细页面,可以传递查询字符串'id'		
public function show_instrument(){
		$this->loging();
		
		$ic=D('Instrument');
		if(isset($_GET['id'])){
			$data=$ic->relation(true)->find($_GET['id']);
		
		}else{
			
			
			$data=$ic->relation(true)->find();
		}
	
		$time=time();
		$this->assign('time',$time);
		$this->assign('data',$data);
		$this->display();
			
			


			
			
	}	
	//显示所有借用列表
public function show_lists(){

		
		$lists=D('Lendinglist');
		$data=$lists->relation(true)->order('stime desc,btime desc')->select();
		$iterm=1;
		
		$time=time();
		$this->assign('time',$time);
		$this->assign('data',$data);
		$this->assign('iterm',$iterm);
		//dump($data);
		$this->display();
			
			


			
			
	}	
	//借用仪器是否成功
	public function borrow_instrument(){
		if(session('?username')&&session('username')!=''&&isset($_POST['iid'])&&session('?uid')){
			$iid = $_POST['iid'];
		
		$username = session('username');
			
		$uid = session('uid');
		
		
		
		$data['userid']=$uid;
		$data['inid']=$iid;
		$data['btime']=time();
		$data['bat']=$_POST['bat'];
		$data['lstatus']=0;
		$ls=M('lendinglist');
		$num=$ls->add($data);
		$idata['id']=$iid;
		$idata['istatus']=1;
		
		$in=M('instrument');
		$inum=$in->save($idata);
		
		if($num>0&&$inum>0){
			
			$this->success('借用成功！！！');
			
		}else{
			
			$this->error('生成借用清单失败');
		}
		}else{
			$this->error('借用失败');
		}
		
	}
//归还仪器是否成功	,post
	public function give_back(){

		
		
			
			


		
	
			
	}		
//显示所有仪器		
	public function show_inlists(){

		
		
			
			


		
	
			
	}			
		
//显示所有仪器		
	public function show_my_inlist(){

		
		
			
			


		
	
			
	}		
	
}